package com.learn.netty.config;

import java.net.InetSocketAddress;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import com.learn.netty.config.netty.ChatServer;

import io.netty.channel.ChannelFuture;

/**
 * 
 * @author kaige
 *
 */
@Component
public class CmsApplicationRunner implements ApplicationRunner {
	
	@Autowired
	private ChatServer chatServer;

    @Override
    public void run(ApplicationArguments  var1) throws Exception{
		InetSocketAddress address = new InetSocketAddress("localhost", 8888);
		ChannelFuture future = chatServer.start(address);
		System.out.println("chatServer start success");
		
		Runtime.getRuntime().addShutdownHook(new Thread(){
			@Override
			public void run() {
				chatServer.destroy();
			}
		});
		
		future.channel().closeFuture().syncUninterruptibly();
    }
}